﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Domain.Models;
using System.Data;
using Utilities;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace Domain.DAL
{
    public class StatusDAO
    {
        #region Private Fields

        private Database db;

        #endregion

        #region Constructors

        public StatusDAO(Database theDatabase)
        {
            this.db = theDatabase;
        }

        #endregion

        #region Public Methods

        public Status Get(int id)
        {
            Status theStatus = new Status();
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_Status_Get", id);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    if(ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"] != DBNull.Value)
                    {
                        theStatus.ID = Convert.ToInt32(ds.Tables[0].Rows[0]["ID"]);
                    }
                    if(ds.Tables[0].Rows[0]["StatusName"] != null && ds.Tables[0].Rows[0]["StatusName"] != DBNull.Value)
                    {
                        theStatus.StatusName = ds.Tables[0].Rows[0]["StatusName"].ToString();
                    }
                    if(ds.Tables[0].Rows[0]["IsStart"] != null && ds.Tables[0].Rows[0]["IsStart"] != DBNull.Value)
                    {
                        theStatus.IsStart = ds.Tables[0].Rows[0]["IsStart"].ToString();
                    }
                    if(ds.Tables[0].Rows[0]["IsEnd"] != null && ds.Tables[0].Rows[0]["IsEnd"] != DBNull.Value)
                    {
                        theStatus.IsEnd = ds.Tables[0].Rows[0]["IsEnd"].ToString();
                    }
                    if(ds.Tables[0].Rows[0]["IsDeleted"] != null && ds.Tables[0].Rows[0]["IsDeleted"] != DBNull.Value)
                    {
                        theStatus.IsDeleted = ds.Tables[0].Rows[0]["IsDeleted"].ToString();
                    }
                    
                }
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return theStatus;
        }

        public Status GetStartStatus()
        {
            Status theStatus = new Status();
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_Status_GetStartStatus");
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    if(ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"] != DBNull.Value)
                    {
                        theStatus.ID = Convert.ToInt32(ds.Tables[0].Rows[0]["ID"]);
                    }
                    if(ds.Tables[0].Rows[0]["StatusName"] != null && ds.Tables[0].Rows[0]["StatusName"] != DBNull.Value)
                    {
                        theStatus.StatusName = ds.Tables[0].Rows[0]["StatusName"].ToString();
                    }
                    if(ds.Tables[0].Rows[0]["IsStart"] != null && ds.Tables[0].Rows[0]["IsStart"] != DBNull.Value)
                    {
                        theStatus.IsStart = ds.Tables[0].Rows[0]["IsStart"].ToString();
                    }
                    if(ds.Tables[0].Rows[0]["IsEnd"] != null && ds.Tables[0].Rows[0]["IsEnd"] != DBNull.Value)
                    {
                        theStatus.IsEnd = ds.Tables[0].Rows[0]["IsEnd"].ToString();
                    }
                    if(ds.Tables[0].Rows[0]["IsDeleted"] != null && ds.Tables[0].Rows[0]["IsDeleted"] != DBNull.Value)
                    {
                        theStatus.IsDeleted = ds.Tables[0].Rows[0]["IsDeleted"].ToString();
                    }
                    
                }
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return theStatus;
        }

        public Status GetEndStatus()
        {
            Status theStatus = new Status();
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_Status_GetEndStatus");
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"] != DBNull.Value)
                    {
                        theStatus.ID = Convert.ToInt32(ds.Tables[0].Rows[0]["ID"]);
                    }
                    if (ds.Tables[0].Rows[0]["StatusName"] != null && ds.Tables[0].Rows[0]["StatusName"] != DBNull.Value)
                    {
                        theStatus.StatusName = ds.Tables[0].Rows[0]["StatusName"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["IsStart"] != null && ds.Tables[0].Rows[0]["IsStart"] != DBNull.Value)
                    {
                        theStatus.IsStart = ds.Tables[0].Rows[0]["IsStart"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["IsEnd"] != null && ds.Tables[0].Rows[0]["IsEnd"] != DBNull.Value)
                    {
                        theStatus.IsEnd = ds.Tables[0].Rows[0]["IsEnd"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["IsDeleted"] != null && ds.Tables[0].Rows[0]["IsDeleted"] != DBNull.Value)
                    {
                        theStatus.IsDeleted = ds.Tables[0].Rows[0]["IsDeleted"].ToString();
                    }

                }
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return theStatus;
        }

        public int Insert(Status theStatus)
        {
            int result = MyConfig.NullNumber;

            try
            {
                result = Convert.ToInt32(this.db.ExecuteScalar("sp_Status_Insert", theStatus.StatusName, theStatus.IsStart, theStatus.IsEnd));
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        public bool Update(Status theStatus)
        {
            bool result = false;

            try
            {
                result = Convert.ToInt32(this.db.ExecuteScalar("sp_Status_Update", theStatus.ID, theStatus.StatusName, theStatus.IsStart, theStatus.IsEnd)) == theStatus.ID;
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        public bool Delete(string id)
        {
            bool result = false;

            try
            {
                result = this.db.ExecuteScalar("sp_Status_Delete", id.ToString()).ToString() == id;
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        public DataSet Search(Status theStatus)
        {
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_Status_List", theStatus.ID, theStatus.StatusName, theStatus.IsStart, theStatus.IsEnd);
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return ds;
        }

        public DataSet ListNoEndStatus()
        {
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_Status_ListNoEndStatus");
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return ds;
        }

        #endregion 

        #region Public Static Methods

        public static StatusDAO GetInstance()
        {
            return EnterpriseLibraryContainer.Current.GetInstance<StatusDAO>();
        }

        #endregion
    }
}
